package edu.ccut.titan.core.impl;

import java.util.List;

import org.osgi.framework.Bundle;

import edu.ccut.titan.component.ComponentFactory;
import edu.ccut.titan.component.IDirectoryInfo;
import edu.ccut.titan.component.TitanComponentException;
import edu.ccut.titan.core.impl.resources.WebResourceResolver;
import edu.ccut.titan.service.ITitanConstants;
import edu.ccut.titan.service.ITitanPropertiesService;

public class ResourcesResolver {

	private String webRootBundleDirectory = null;

	public ResourcesResolver() {

	}

	public void loadResource(Bundle bundle,
			ITitanPropertiesService titanPropertiesService)
			throws TitanComponentException {

		String webRootPath = titanPropertiesService
				.getProperty(ITitanConstants.TITAN_SERVER_LOCATION);

		this.webRootBundleDirectory = webRootPath.replace("\\", "/") + "/"
				+ bundle.getSymbolicName();

		List<IDirectoryInfo> directoryInfos = ComponentFactory
				.getDirectoryInfos(bundle);

		for (IDirectoryInfo directoryInfo : directoryInfos) {
			if (IDirectoryInfo.TITAN_DIRECTORY_TYPE_WEB.equals(directoryInfo
					.getType())) {
				WebResourceResolver.deploy(bundle, directoryInfo
						.getDirectoryPath(), webRootBundleDirectory);
			}
		}
	}

	public void unloadResource(Bundle bundle,
			ITitanPropertiesService titanPropertiesService)
			throws TitanComponentException {
		
		WebResourceResolver.undeploy(this.webRootBundleDirectory);
	}

}
